﻿using System;
using System.Web.Services;
using System.Xml;

using umlungu.engineX;
using Joshi.Utils.Imap;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2008-04-20
	Status:		beta
	Version:	2.0.0
	Build:		20080420
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20080420:	Started. Depends on ImapLibrary.dll by Rohit Joshi
	-----------------------------------------------------------------------	*/

namespace umlungu.engineX.emailX {
	/// <summary>
	/// umlungu imap web services
	/// </summary>
	[WebService(Namespace = "http://www.umlungu.com/engineX/")]
	public class imapX : x_result {
		#region Private properties
		#endregion

		#region Constants
		private const string FOLDER_DEFAULT = "INBOX";
		#endregion

		#region Visible properties
		private x_imap imap;
		/// <summary>The SMTP server to be used to send a message</summary>
		/// <value>IP or named address</value>
		protected x_imap ImapX {
			get { return imap = new x_imap(); }
		}
		#endregion

		#region Protected methods
		/// <summary>Constructor</summary>
		public imapX() : base("imapX") {
		}
		#endregion

		#region web service methods
		[WebMethod(Description="Login to an IMAP server")]
		public XmlDocument Login(string Host, string Username, string Password, string UseSSL) {
			using (ImapX) {
				try {
					imap.Login(Host, Username, Password, UseSSL.Trim().ToLower().Equals("true") ? true : false);
					_AddOk();
				}
				catch (ImapException e)	{ _AddError(e); }
				catch (x_exception e)	{ _AddError(e); }
				catch (Exception e)		{ _AddError(e); }
			}
			return (Result);
		}


		[WebMethod(Description = "Count messages that are in the default folder (ie 'INBOX')")]
		public XmlDocument MessageCount(string Username, string Password) {
			using (ImapX) {
				try {
					imap.ExamineFolder(Username, Password, FOLDER_DEFAULT);
					_AddOk();
					_Add("Value", imap.TotalMessages.ToString(), "name", "Total");
					_Add("Value", imap.RecentMessages.ToString(), "name", "Recent");
					//AddElem("Total", imap.TotalMessages.ToString());
					//AddElem("Recent", imap.RecentMessages.ToString());
				}
				catch (ImapException e) { _AddError(e); }
				catch (x_exception e) { _AddError(e); }
				catch (Exception e) { _AddError(e); }
			}
			return (Result);
		}


		[WebMethod(Description = "Count messages that are in a folder")]
		public XmlDocument MessageCountFolder(string Username, string Password, string Folder) {
			using (ImapX) {
				try {
					imap.ExamineFolder(Username, Password, Folder);
					_AddOk();
					AddElem("Mailbox", imap.MailboxName);
					_Add("Value", imap.TotalMessages.ToString(), "name", "Total");
					_Add("Value", imap.RecentMessages.ToString(), "name", "Recent");
					//AddElem("Total", imap.TotalMessages.ToString());
					//AddElem("Recent", imap.RecentMessages.ToString());
				}
				catch (ImapException e) { _AddError(e); }
				catch (x_exception e) { _AddError(e); }
				catch (Exception e) { _AddError(e); }
			}
			return (Result);
		}
		#endregion
	}
}
